/*
 * 前后端数据通讯方法
 */
var cAjax = function(settings) {
	
	var reg = /":(\d{16,})/gm;
	
	var ERROR={
			no_permission:4010
		};
	function callback(obj) {
		//处理长整型被js截取精度问题
		obj = obj.replace(reg,"\":\"$1\"");
		obj = JSON.parse(obj);
		
		var code = obj['errorCode'];
		if( code == undefined) {
			showError({
				code: -6,
				msg: '服务器返回数据格式不正确'
			});
		} else if(code == 0) {
			settings.success && settings.success(obj);		
		} else {
			switch (code) {
			case ERROR.no_permission:
				var per = obj['data'];
				if(per!=undefined){
					showError({msg:'你没有 '+per.name+' 权限'});
				}else{
					showError({msg:'你没有权限'});
				}
				return;
			default:
				break;
			}
			if( settings.error == undefined ) {
				showError(obj);
			} else {
				settings.error(obj);
			}
		}
	}

	/*
	 * 默认错误提示
	 */
	function showError(obj) {
		
		//处理长整型被js截取精度问题
//		obj = obj.replace(reg,"\":\"$1\"");
//		obj = JSON.parse(obj);
		
		var modalId = 'cms-error-modal';
		var modalTplHtml = '<div id="'+modalId+'" class="modal modal-warning fade "> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title">发生错误</h4> </div> <div class="modal-body"> <p>One fine body…</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">关闭</button> <button type="button" class="btn btn-outline" data-dismiss="modal">确定</button> </div> </div></div></div>';
		var modalELe = null;
		if($('#'+modalId).length == 0) {
			$('body').append($(modalTplHtml));
			modalEle = $('#'+modalId);
			modalEle.modal({
				backdrop: 'static',
				keyboard: false,	
				show: false
			});
		} else {
			modalELe = $('#'+modalId);
		}
		$('.modal-body',modalEle).html(obj.msg);
		modalEle.modal('show');
	}

	$.ajax({
		url: settings.url || window.location.href,
		type: settings.type || "post",
		data: settings.data,
		traditional:settings.traditional || true,
		dataType: "text",
		beforeSend:function(request){request.setRequestHeader("requestType", "ajax");},
		success: callback,
		error: function(xhr, status) {
			var map = {
				"abort": {
					code: -1,
					msg: "网络请求被取消。"
				},
				"parsererror": {
					code: -2,
					msg: "网络返回解析错误。"
				},
				"timeout": {
					code: -3,
					msg: "网络请求超时。"
				},
				"error": {
					code: -4,
					msg: "网络错误。"
				}
			};
			var result = map[status];
			if (!result) {
				result = {
					code: -5,
					msg: "未知的网络错误。"
				};
			}
			showError(result);
		},
		complete: settings.complete	
	});	
};

/*
 * 操作确认 modal
 */
var actionConfirm = function(settings) {
	var modalId = 'cms-confirm-modal';
	var modalTplHtml = '<div id="'+modalId+'" class="modal modal-default fade "> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title">提示</h4> </div> <div class="modal-body"> <p>One fine body…</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default pull-left" data-dismiss="modal">取消</button> <button type="button" class="btn btn-warning confirm-btn" data-dismiss="modal">确定</button> </div> </div></div></div>';
	var modalEle = null;
	if($('#'+modalId).length == 0) {
		$('body').append($(modalTplHtml));
		modalEle = $('#'+modalId);
		modalEle.modal({
			backdrop: 'static',
			keyboard: false,	
			show: false
		});
	} else {
		modalEle = $('#'+modalId);
	}
	$('.modal-body',modalEle).html(settings.msg);
	$('.confirm-btn',modalEle).off('click').on('click',function(){
		settings['confirm']();
		modalEle.modal('hide');
	});
	modalEle.modal('show');
}

/*
 * 操作成功提示 modal
 */
var greeting = function(settings) {
	var modalId = 'cms-greeting-modal';
	var modalTplHtml = '<div id="'+modalId+'" class="modal modal-default fade "> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h4 class="modal-title">操作成功</h4> </div> <div class="modal-body"> <p>One fine body…</p> </div> <div class="modal-footer">  <button type="button" class="btn btn-success confirm-btn" data-dismiss="modal">知道了</button> </div> </div></div></div>';
	var modalEle = null;
	if($('#'+modalId).length == 0) {
		$('body').append($(modalTplHtml));
		modalEle = $('#'+modalId);
		modalEle.modal({
			backdrop: 'static',
			keyboard: false,	
			show: false
		});
	} else {
		modalEle = $('#'+modalId);
	}
	$('.modal-body',modalEle).html(settings.msg);
	$('.modal-title',modalEle).html(settings['title']?settings['title']:'操作成功');
	if(settings['confirm']) {
		$('.confirm-btn',modalEle).off('click').on('click',function(){
			settings['confirm']();
			modalEle.modal('hide');
		});
	}	
	modalEle.modal('show');
}

/*
 * 操作警告
 */
var alerting = function(settings) {
	var modalId = 'cms-alerting-modal';
	var modalTplHtml = '<div id="'+modalId+'" class="modal modal-default fade "> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header  alert-warning"> <h4 class="modal-title">警告</h4> </div> <div class="modal-body"> <p>One fine body…</p> </div> <div class="modal-footer">  <button type="button" class="btn btn-warning confirm-btn" data-dismiss="modal">知道了</button> </div> </div></div></div>';
	var modalEle = null;
	if($('#'+modalId).length == 0) {
		$('body').append($(modalTplHtml));
		modalEle = $('#'+modalId);
		modalEle.modal({
			backdrop: 'static',
			keyboard: false,	
			show: false
		});
	} else {
		modalEle = $('#'+modalId);
	}
	$('.modal-body',modalEle).html(settings.msg);
	$('.modal-title',modalEle).html(settings['title']?settings['title']:'警告');
	if(settings['confirm']) {
		$('.confirm-btn',modalEle).off('click').on('click',function(){
			settings['confirm']();
			modalEle.modal('hide');
		});
	}	
	modalEle.modal('show');
}


/*
 * 图片 upload 
 */
/*
 * settting字段
 * before: 上传前调用的函数
 * sucess: 上传成功后调用的函数
 */
function PicUploader (settings) {
	var inputId = 'picupload-'+Math.random().toString().slice(2);		
	var self = this;
	this.settings = settings;
	this.$fileInput = $('<input type="file" accept=".jpg,.png,.jpeg,.gif" id="'+inputId+'" style="display:none;">');
	this.$fileInput.on('change',function(){
		self._onChange(self);
	});
	$('body').append(this.$fileInput);
}

PicUploader.prototype = {
	consotructor: PicUploader,
	$fileInput: null,
	settings: {},
	/*
	 * 开始上传操作
	 */
	start: function() {
		this.$fileInput.click();
	},
	_onChange:function(self) {
		var picFormData = new FormData();
		picFormData.append('file',this.$fileInput.get(0).files[0]);
		/*
		 * 上传前
		 */
		self.settings['before'] && self.settings['before']();

		$.ajax({
			url: '/base/upload/uploadFile',
			type: 'post',
			data: picFormData,
			dataType: 'json',
			cache: false,
			contentType: false,
			processData: false,
			success: function(obj) {
				if(obj.errorCode == 0) {
					/*
					 * 上传成功
					 */
					self.settings['success'] && self.settings['success'](obj);
				} else {
				}
		   	}
		});
    }
}

/*js定义常量*/
var JSConst = (function() {
	var c = {
		ZHISLAND_TYPE_BLUE:1,//蓝岛
		ZHISLAND_TYPE_GREEN:2,//绿岛		
		USER_BASE_RANK_DING:100,//岛丁用户
		USER_BASE_RANK_PRE:200,//标准用户
		USER_BASE_RANK_INVITE:300,//优质用户
		USER_BASE_RANK_ISLAND:400//岛邻用户
	}
	var T={};
	T.get=function(n){
		return c[n];
	}
	return T;
})();
//var k=JSConst.get('ZHISLAND_TYPE_BLUE');alert(k);
